﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MethodWorx.CMS.Services;
using MethodWorx.CMS.Content;
using MethodWorx.CMS.Plugins;
using MethodWorx.CMS.Administration;
using MethodWorx.CMS.Plugins.ContentTree;

namespace MethodWorx.CMS.UI.Areas.Application.Controllers
{
    public class ContentTreeController : Controller
    {
        private ContentService content;
        private SchemaService schema;
        private PluginFactory plugins;
        //
        // GET: /ContentTree/
        public ContentTreeController(ContentService content, SchemaService schema, IApplicationSettingsRepository settings, IPluginRepository plugins)
        {
            this.content = content;
            this.schema = schema;
            this.plugins = new PluginFactory(settings, plugins);
        }

        [HttpGet]
        public JsonResult ContentStructure(string objectType, Guid? objectId)
        {
            List<object> nodes = new List<object>();

            //
            //  ok, iterate through each plugin
            foreach (SafePlugin sp in this.plugins.GetContentTreePlugins())
            {
                foreach(var treeNode in sp.Invoke<IContentTreePlugin, IEnumerable<TreeNode>>(p => p.GetTreeNodes(objectType, objectId) ?? new TreeNode [] { }))
                {
                    nodes.Add(TreeNodeToUIObject(treeNode));
                }
            }

            return Json(nodes.ToArray(), JsonRequestBehavior.AllowGet);
        }

        private object TreeNodeToUIObject(TreeNode treeNode)
        {
            treeNode.Icon = treeNode.Icon ?? "";
            return new
                    {
                        attr = new { objectType = treeNode.ObjectType, objectId = treeNode.ObjectId },
                        data = new { title = treeNode.Title, icon = Url.Content(treeNode.Icon) },
                        state = "closed",
                        metadata = new { menuItems = treeNode.MenuItems, toolbarButtons = SetRelativeUrlsFromButtons(treeNode.ToolbarButtons), defaultAction=treeNode.DefaultAction }
                    };
        }

        private ToolbarButton[] SetRelativeUrlsFromButtons(ToolbarButton[] toolbarButton)
        {
            if (toolbarButton != null)
            {
                foreach (var button in toolbarButton)
                {
                    button.Icon = button.Icon ?? "";
                    button.Icon = Url.Content(button.Icon);
                }
            }
            return toolbarButton;
        }

        public ActionResult InvokeAction(string actionId, string objectType, Guid? objectId)
        {
            foreach (SafePlugin sp in this.plugins.GetContentTreePlugins())
            {
                var result = sp.Invoke<IContentTreePlugin, ActionResult>(p => p.InvokeAction(actionId, objectType, objectId));
                if (result != null && !(result is EmptyResult))
                    return result;
            }

            return new EmptyResult();
        }

    }
}
